const { BrowserWindow, Menu, app, dialog } = require('electron')
const fs = require('fs-extra')
const path = require('path')
const { file } = require('electron-settings')

let template = [{
    label: '文件',
    submenu: [{
        label: '打开图片文件',
        accelerator: 'CmdOrCtrl+Shift+O',
        role: 'openFile',
        click: () => {
            dialog.showOpenDialog({
                properties: ['openFile'],
                filters: [{ name: '序列帧配置', extensions: ['jpg'] }]
            }, files => {
                if (!files || !files.length) return;
                sendMsg('importFile', files.shift())
            })
        }
    },
    {
        label: '打开地图目录',
        accelerator: 'CmdOrCtrl+O',
        click: () => {
            dialog.showOpenDialog({
                properties: ['openDirectory']
            }, files => {
                if (!files || !files.length) return;
                sendMsg('importDir', files.shift())
            })
        }
    },
    {
        type: 'separator'
    },
    {
        label: '刷新',
        accelerator: 'F5',
        click: () => {
            sendMsg('refresh')
        }
    },
    {
        label: '保存',
        accelerator: 'CmdOrCtrl+S',
        click: () => {
            sendMsg('saveData')
        }
    },
    {
        type: 'separator'
    },
    {
        label: '关闭',
        accelerator: 'CmdOrCtrl+W',
        role: 'close'
    }
    ]
},
{
    label: '查看',
    submenu: [{
        label: '全屏',
        accelerator: (() => {
            if (process.platform === 'darwin') {
                return 'Ctrl+Command+F'
            } else {
                return 'F11'
            }
        })(),
        click: (item, focusedWindow) => {
            if (focusedWindow) {
                focusedWindow.setFullScreen(!focusedWindow.isFullScreen())
            }
        }
    }, {
        label: '开发者工具',
        accelerator: (() => {
            if (process.platform === 'darwin') {
                return 'Alt+Command+I'
            } else {
                return 'F12'
            }
        })(),
        click: (item, focusedWindow) => {
            if (focusedWindow) {
                focusedWindow.toggleDevTools()
            }
        }
    }]
},
{
    label: '帮助',
    submenu: [{
        label: '版本v0.0.1',
        enabled: false
    }]
}
]

function sendMsg(...args) {
    BrowserWindow.getAllWindows().forEach(w => {
        w.webContents.send('SERVER-MESSAGE', args)
    })
}

app.on('ready', () => {
    const menu = Menu.buildFromTemplate(template)
    Menu.setApplicationMenu(menu)
})

app.on('browser-window-created', () => { })

app.on('window-all-closed', () => { })